<template>
  <div>
    <FilterTable
      ref="table"
      height="auto"
      :dataSource="showRowData"
      selectionType="single"
      :onRowSelectChange="selectChange"
      :defaultSelections="selectedLIST"
      :columns="columns"
      :onColumnsChange="columns => this.columns = columns"
    />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button :disabled="isChoice" @click="!isChoice?deleteAction():''">移 除</el-button>
      <el-button :disabled="isChoice" @click="!isChoice?saveDialog():''" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { combineClue } from '@/api/potenCus/customer/cluesQuery';
import { formatDates } from '@/utils/validate';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'MergeModel',
  mixins: [dictionary],
  props: {
    rowData: {
      type: Array,
      default: ()=>([])
    },
  },
  data () {
    return {
      columns: this.createTableColumns(),
      isChoice: false,
      showRowData: [],
      selectedRows: [],
      selectedLIST: [],
    }
  },
  created () {
    let { rowData } = this
    this.showRowData = JSON.parse(JSON.stringify(rowData))
    console.log('选择要合并的数据',rowData, this.showRowData)
      let nArr = [];
      this.showRowData.forEach(x =>{
        if(x.intentLevel === 15191008){
          nArr.push(x)
        }
      })
      console.log('默认选中', nArr)
      this.selectedLIST = nArr
  },
  mounted(){
    console.log('mounted', this.showRowData)
    let newArr = [];
    this.showRowData.forEach(x =>{
      if(x.intentLevel === 15191008){
        this.$refs.table.SET_DISABLE_SELECT(this.showRowData)
      }
    })
  },
  methods: {
    selectChange (rows){ //选中时触发的函数
      this.selectedRows = rows;
      this.isChoice = this.selectedRows.length > 0? false: true;
    },
    // 确认
    saveDialog () {
      let { selectedRows } = this
      console.log( '选择的主线索',this.showRowData, selectedRows)
      if(selectedRows.length ==0){
        this.$message.info('请选择一条主线索')
      } else{
        let params = this.showRowData.map(item => {
          return {
            customerBusinessId: item.customerBusinessId.toString(),
            isMainClue: item.customerBusinessId == selectedRows[0].customerBusinessId ? '10041001' : '10041002'
          }
        })
        console.log('提交数据',{ combineClueListDTO: params })
        combineClue({ combineClueListDTO: params }).then(res => {
          if (res.success){
            this.$message.success('保存成功')
            this.closeDialog('refresh')
          } else {
            this.$message.error(res.errMsg)
          }
        })
      }
    },
    // 移除
    deleteAction () {
      const arr = []
      if(this.selectedRows.length == 0){
        this.$message.info('请选择要移除的线索')
      } else{
        this.showRowData.map(x=>{
          if(x.potentialCustomersId!=this.selectedRows[0].potentialCustomersId){
            arr.push(x)
          }
        })
        this.selectedRows = []
        this.showRowData = arr;
        console.log('移除后', this.showRowData)
        this.$emit("selected", this.showRowData)
      }
    },
    // 取消
    closeDialog (val = '') {
      this.$emit('close', false, val)
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '创建时间',
          dataIndex: 'createdAt',
          width: 150,
          align: 'center',
          render: props =>{
            return <span>{ formatDates(props.row.createdAt) }</span>
          }
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          align: 'center',
        },
        {
          title: '联系人姓名',
          dataIndex: 'contactName',
          align: 'center',
        },
        {
          title: '性别',
          dataIndex: 'gender',
          align: 'center',
          filterItems: this.createDictList('1002'),
          render: props => {
            return <span>{this.createDictText(props.row.gender, '1002')}</span>;
          }
        },
        {
          title: '手机号码',
          dataIndex: 'mobilePhone',
          align: 'center',
        },
        {
          title: '意向品牌',
          dataIndex: 'inteBrandName',
          align: 'center',
        },
        {
          title: '意向车系',
          dataIndex: 'inteCarSeriesName',
          align: 'center',
        },
        {
          title: '意向车型',
          dataIndex: 'inteCarTypeName',
          align: 'center',
        },
        {
          title: '意向级别(当前)',
          dataIndex: 'intentLevel',
          align: 'center',
          filterType: 'checkbox',
          filterItems: this.createDictList('1519'),
          render: props => {
            return <span>{this.createDictText(props.row.intentLevel, '1519')}</span>;
          }
        },
        {
          title: '是否下发',
          dataIndex: 'manufacturerIssue',
          align: 'center',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.manufacturerIssue, '1004')}</span>;
          }
        },
        {
          title: '是否进店',
          dataIndex: 'shop',
          align: 'center',
          render: props => {
            return <span>{props.row.shop ==true?'是':'否'}</span>;
          }
        },
      ];
    },
  }
}
</script>

<style lang="less" scoped>

</style>
